﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.Member
{
    public class PostPm : BasePage
    {
        protected MessageInfo Pm = new MessageInfo();
        protected string str;

        protected int msgtoid = -1;


        protected override void ShowPage()
        {

            //判断是否是回复或转发

            string action = RequestTools.GetQueryString("action").ToLower();
            if (action.CompareTo("re") == 0 || action.CompareTo("fw") == 0)
            {
                int pmid = RequestTools.GetQueryInt("Pmid", -1);
                if (pmid != -1)
                {
                    MessageInfo Pm2 = MessageBLL.Instance.GetMessage(pmid);
                    if (action.CompareTo("re") == 0)
                    {
                        Pm.Msgto = Pm2.Msgfrom;
                    }
                    else
                    {
                        Pm.Msgto = "";
                    }
                    Pm.Subject = action + ":" + Pm2.Subject;
                    Pm.Message = Pm2.Message;
                }
            }




            #region 创建短消息并发送

            if (RequestTools.IsPost())
            {

                if (!CheckPermissionAfterPost())
                {
                    return;
                }

                
                Pm.Msgto = RequestTools.GetString("msgto");
                Pm.Msgtoid = msgtoid;
                // !!!!!Replace This!!!!!!!!!
                // 现在有的用户的用户名会显示成邮箱，为了解决这个问题，我在此到数据库中取一次用户名
                //Pm.Msgfrom = UserData.UserName;
                Pm.Msgfrom = UserBLL.Instance.GetUserInfo(UserData.Id).UserName;
                Pm.Msgfromid = UserData.Id;
                Pm.Postdatetime = DateTime.Now.ToString();
                Pm.New = 1;
                Pm.Subject = RequestTools.GetString("subject");
                Pm.Message = RequestTools.GetString("message");

                if (RequestTools.GetString("savetodraftbox").Equals("存为草稿"))
                {


                    //// 检查发送人的短消息是否已超过发送人用户组的上限
                    //if (MessageBLL.GetMessageCount(UserInfo.Id, -1) >= usergroupinfo.Maxpmnum)
                    //{
                    //    AddErrLine("抱歉,您的短消息已达到上限,无法保存到草稿箱");
                    //    return;
                    //}


                    Pm.Folder = 2;
                    MessageBLL.Instance.CreateMessage(Pm, 0);//创建短消息，"0"代表不保存到发件箱
                    SetUrl("PmDraftbox.aspx");
                    SetMetaRefresh();
                   // SetShowBackLink(true);
                    this.AddMsgLine("已将消息保存到草稿箱");
                }

                else if (RequestTools.GetString("savetosentbox").Equals("留在发件箱"))
                {

                    //// 检查接收人的短消息是否已超过接收人用户组的上限
                    //UserInfo touser = Users.GetUserInfo(msgtoid);
                    //if (PrivateMessages.GetPrivateMessageCount(msgtoid, -1) >=
                    //    UserGroups.GetUserGroupInfo(touser.Groupid).Maxpmnum)
                    //{
                    //    AddErrLine("抱歉,接收人的短消息已达到上限,无法接收");
                    //    return;
                    //}



                    MessageBLL.Instance.CreateMessage(Pm, 1);//创建短消息，"1"代表保存到发件箱
                    SetUrl("PmSentbox.aspx");
                    SetMetaRefresh();
                   // SetShowBackLink(true);
                    this.AddMsgLine("发送完毕，已将消息保存到发件箱");
                }
                else
                {

                    //// 检查接收人的短消息是否已超过接收人用户组的上限
                    //if (PrivateMessages.GetPrivateMessageCount(msgtoid, -1) >=
                    //    UserGroups.GetUserGroupInfo(touser.Groupid).Maxpmnum)
                    //{
                    //    AddErrLine("抱歉,接收人的短消息已达到上限,无法接收");
                    //    return;
                    //}



                    MessageBLL.Instance.CreateMessage(Pm, 0);//创建短消息，"0"代表不保存到发件箱
               
                    //SetContinueToWrite(true);
                    this.AddMsgLine("已成功发送！");
                }
            }

            #endregion 

        }

        #region 发送后提交后检查
        private bool CheckPermissionAfterPost()
        {
            if (RequestTools.GetString("msgto").Equals(""))
            {
                AddErrLine("接收人不能为空");

                return false;
            }


            string msgto = RequestTools.GetString("msgto");
            if(msgto != "") {
                UserInfo msgtoUserInfo = UserBLL.Instance.GetUserInfo(msgto);
                if (msgtoUserInfo != null) msgtoid = msgtoUserInfo.Id;             
            }

            if (msgtoid == -1)
            {
                AddErrLine("接收人不是注册用户");
                return false;
            }

            if (RequestTools.GetString("subject").Trim().Length > 60)
            {
                AddErrLine("标题不能超过60字");

                return false;
            }

            if (RequestTools.GetString("subject").Equals(""))
            {
                Pm.Subject = "无标题";
            }

            if (RequestTools.GetString("message").Equals(""))
            {
                this.AddErrLine("内容不能为空");

                return false;
            }

            if (RequestTools.GetString("message").Length > 3000)
            {
                AddErrLine("内容不能超过3000字");

                return false;
            }

            return true;
        }
        #endregion


    }
}
